$(function() {
	// 页面控件
	var $iChecks = $('.i-checks'); // 页面中的所有checkbox（iCheck）
	
	var $shelfTabWarning = $('#shelfTabWarning'); // 按机架：警告框
	var $byShelfForm = $('#byShelfForm'); // 按机架表单
	var $firmwareTypeId_shelf = $('#firmwareTypeId_shelf'); // 按机架：所选固件的矿机类型ID
	var $shelfTypeId_shelf = $('#shelfTypeId_shelf'); // 按机架：所选机架的矿机类型ID
	var $firmwareNameDisplay_shelf = $('#firmwareNameDisplay_shelf'); // 按机架：所选固件名称-显示用
	var $firmwareNameTip_shelf = $('#firmwareNameTip_shelf'); // 按机架：未选择固件时的提示
	var $firmwareVersionDisplay_shelf = $('#firmwareVersionDisplay_shelf'); // 按机架：所选固件的版本-显示用
	var $firmwareTypeDisplay_shelf = $('#firmwareTypeDisplay_shelf'); // 按机架：所选固件的矿机类型-显示用
	var $shelfNameDisplay_shelf = $('#shelfNameDisplay_shelf'); // 按机架：所选机架名称-显示用
	var $shelfNameTip_shelf = $('#shelfNameTip_shelf'); // 按机架：未选择机架时的提示
	var $ipBlockDisplay_shelf = $('#ipBlockDisplay_shelf'); // 按机架：所选机架的IP段-显示用
	var $shelfTypeDisplay_shelf = $('#shelfTypeDisplay_shelf'); // 按机架：所选机架的矿机类型-显示用
	
	var $firmwareId_shelf = $('#firmwareId_shelf');// 按机架表单:所选固件的ID
	var $minerShelfId_shelf = $('#minerShelfId_shelf'); // 按机架表单：所选机架的ID
	var $minerShelfName_shelf = $('#minerShelfName_shelf'); // 按机架表单：所选机架的名称
	var $ipBlock_shelf = $('#ipBlock_shelf'); // 按机架表单：所选机架的IP段
	var $ipBeginning_shelf = $('#ipBeginning_shelf'); // 按机架表单：IP起始索引
	var $ipEnd_shelf = $('#ipEnd_shelf'); // 按机架表单：IP结束索引
	var $ignores_shelf = $('#ignores_shelf'); // 按机架表单： “忽略的IP索引”下拉列表框
	var $clean_shelf = $('#clean_shelf'); // 按机架表单：清空配置
	var $force_shelf = $('#force_shelf'); // 按机架表单：升级方式
	
	var $btnSelectFirmware_shelf = $('#btnSelectFirmware_shelf'); // 按机架：固件选择按钮
	var $btnSelectShelf_shelf = $('#btnSelectShelf_shelf'); // 按机架：机架按钮
	var $btnReset_shelf = $('#btnReset_shelf'); // 按机架：清空按钮
	var $btnSave_shelf = $('#btnSave_shelf'); // 按机架：提交按钮
	
	var $byIPForm = $('#byIPForm'); // 按IP表单
	var $firmwareTypeId_ip = $('#firmwareTypeId_ip'); // 按IP：所选固件ID
	var $firmwareNameDisplay_ip =$('#firmwareNameDisplay_ip'); // 按IP：所选固件的名称-显示用
	var $firmwareNameTip_ip = $('#firmwareNameTip_ip'); // 按IP：未选择固件时的提示
	var $firmwareVersionDisplay_ip = $('#firmwareVersionDisplay_ip'); // 按IP：所选固件的版本-显示用
	var $firmwareTypeDisplay_ip = $('#firmwareTypeDisplay_ip'); // 按IP：所选固件的矿机类型-显示用
	var $ipListTip_ip = $('#ipListTip_ip'); // 按IP：未选择IP文件时的提示
	
	var $firmwareId_ip = $('#firmwareId_ip'); // 按IP表单：所选固件的ID
	var $clean_ip = $('#clean_ip'); // 按IP表单：清空配置
	var $force_ip = $('#force_ip'); // 按IP表单：升级方式
	
	var $btnSelectFirmware_ip = $('#btnSelectFirmware_ip'); // 按IP：固件选择按钮
	var $btnImportIpList_ip = $('#btnImportIpList_ip'); // 按IP：从文件导入IP按钮
	var $btnReset_ip = $('#btnReset_ip'); // 按IP：清空按钮
	var $btnSave_ip = $('#btnSave_ip'); // 按IP：提交按钮
	
	var uploader; // 按IP：IP文件选择
	var $fileNameDisplay = $('#fileNameDisplay'); // 按IP：显示所选文件名
	
	var $firmwareDialog = $('#firmwareDialog'); // 固件选择窗口
	var $firmwareTable = $('#firmwareTable'); // 固件选择表格
	var $chosenFirmwareTypeList = $('#chosenFirmwareTypeList'); // 固件现则表格查询条件：矿机类型
	var $inputFirmwareName = $('#inputFirmwareName'); // 固件选择表格查询条件：固件名称
	var $btnFirmwareSelect = $('#btnFirmwareSelect'); // 固件选择窗口：确认选择按钮
	
	var $shelfDialog = $('#shelfDialog'); // 机架选择窗口
	var $shelfTable = $('#shelfTable'); // 机架选择表格
	var $inputShelfName = $('#inputShelfName'); // 机架选择表格查询条件：机架名称
	var $inputShelfIPBlock = $('#inputShelfIPBlock'); // 机架选择表格查询条件：IP段
	var $chosenShelfAreaList = $('#chosenShelfAreaList'); // 机架选择表格查询条件：所属矿区
	var $chosenShelfTypeList = $('#chosenShelfTypeList'); // 机架选择表格查询条件：矿机类型
	var $chosenShelfEnableList = $('#chosenShelfEnableList'); // 机架选择表格查询条件：可用状态
	var $btnShelfSelect = $('#btnShelfSelect'); // 机架选择窗口：确认选择按钮
	
	// 1：按机架；2：按IP
	var by = 0;
	
	initWidgets();
	initLinstener();
	initValidators();
	initUploader();
	
	function doSubmitByShelf() {
		var data = $byShelfForm.serializeObject();
		data.minerSource = 1;
		if (data.clean) {
			data.clean = true;
		} else {
			data.clean = false;
		}
		if ($force_shelf.prop('checked') == true) {
			data.force = true;
		} else {
			data.force = false;
		}
		if (data.ignores && !$.isArray(data.ignores)) {
			var arr = new Array();
			arr[0] = data.ignores;
			data.ignores = arr;
		}
		var paramData = JSON.stringify(data);
		doPostByShelf(paramData);
	}
	
	function doSubmitByIP() {
		var data = $byIPForm.serializeObject();
		if (data.clean) {
			data.clean = true;
		} else {
			data.clean = false;
		}
		if ($force_shelf.prop('checked') == true) {
			data.force = true;
		} else {
			data.force = false;
		}
		doPostByIP(data);
	}
	
	function doPostByShelf(data) {
		loadmask.showProgress('正在提交，请稍后...');
		$.ajax({
			url: 'updates/shelf',
			type: 'POST',
			dataType: 'json',
			contentType: "application/json; charset=utf-8",
			data: data,
			success: function(result) {
				if (result.success) {
					toastr.success(result.message, '提交成功');
				} else {
					toastr.error(result.message, '提交失败');
				}
				loadmask.hide();
			},
			error: function(request, status, error) {
				toastr.error(error, '异常');
				loadmask.hide();
			}
		});
	}
	
	function doPostByIP(data) {
		uploader.option('formData', {
			firmwareId: data.firmwareId,
			clean: data.clean,
			force: data.force,
		});
		loadmask.showProgress('正在提交，请稍后...');
		uploader.upload();
	}
	
	// 比较所选固件的矿机类型和机架矿机类型是否一致，不一致则进行警告提示
	function compareMinerType() {
		var firmwareTypeId = $firmwareTypeId_shelf.val();
		var shelfTypeId = $shelfTypeId_shelf.val();
		if (firmwareTypeId && shelfTypeId) {
			if (firmwareTypeId != shelfTypeId) {
				$shelfTabWarning.show();
				return;
			}
		}
		$shelfTabWarning.hide();
	}
	
	function firmwareSelected(firmware) {
		if (by == 1) {
			$firmwareTypeId_shelf.val(firmware.minerTypeId);
			$firmwareNameDisplay_shelf.text(firmware.name);
			$firmwareVersionDisplay_shelf.text(firmware.version);
			$firmwareTypeDisplay_shelf.text(firmware.minerTypeName);
			$firmwareId_shelf.val(firmware.id);
			$firmwareNameTip_shelf.hide();
			compareMinerType();
		} else if (by == 2) {
			$firmwareNameDisplay_ip.text(firmware.name);
			$firmwareVersionDisplay_ip.text(firmware.version);
			$firmwareTypeDisplay_ip.text(firmware.minerTypeName);
			$firmwareId_ip.val(firmware.id);
			$firmwareNameTip_ip.hide();
		}
	}
	
	var selectedShelf;
	function shelfSelected(shelf) {
		$shelfTypeId_shelf.val(shelf.minerTypeId);
		$shelfNameDisplay_shelf.text(shelf.name);
		$ipBlockDisplay_shelf.text(shelf.ipBlock);
		$shelfTypeDisplay_shelf.text(shelf.minerTypeName);
		$minerShelfId_shelf.val(shelf.id);
		$minerShelfName_shelf.val(shelf.name);
		$ipBlock_shelf.val(shelf.ipBlock);
		$ipBeginning_shelf.val(shelf.ipBeginning);
		$ipEnd_shelf.val(shelf.ipEnd);
		buildIgnoresChosen(shelf.ignores);
		$shelfNameTip_shelf.hide();
		$byShelfForm.validate().form();
		compareMinerType();
		selectedShelf = shelf;
	}
	
	// 根据IP起始和结束索引重建忽略的IP索引下拉列表框
	function buildIgnoresChosen(values) {
		var ipBeginning = $ipBeginning_shelf.val();
		var ipEnd = $ipEnd_shelf.val();
		if (!ipBeginning || !$.isNumeric(ipBeginning) || !ipEnd || !$.isNumeric(ipEnd)) {
			// IP起始和结束索引为空或非数字时置空忽略的IP索引下拉列表框
			$ignores_shelf.empty();
			$ignores_shelf.append($('<option value=""></option>'));
			$ignores_shelf.chosen('destroy').chosen({width:'100%'});
			return;
		}
		
		var ipBeginningShelf = 1;
		var ipEndShelf = 254;
		if (selectedShelf) {
			ipBeginningShelf = selectedShelf.ipBeginning;
			ipEndShelf = selectedShelf.ipEnd;
		}
		
		ipBeginning = parseInt(ipBeginning);
		ipEnd = parseInt(ipEnd);
		
		// IP索引需在机架的IP索引范围之内，且起始索引不能大于结束索引
		if (ipBeginning < ipBeginningShelf) {
			ipBeginning = ipBeginningShelf;
			$ipBeginning_shelf.val(ipBeginning);
		}
		if (ipBeginning > ipEndShelf) {
			ipBeginning = ipEndShelf;
			$ipBeginning_shelf.val(ipBeginning);
		}
		if (ipEnd < ipBeginningShelf) {
			ipEnd = ipBeginningShelf;
			$ipEnd_shelf.val(ipEnd);
		}
		if (ipEnd > ipEndShelf) {
			ipEnd = ipEndShelf;
			$ipEnd_shelf.val(ipEnd);
		}
		if (ipBeginning > ipEnd) {
			var temp = ipBeginning;
			ipBeginning = ipEnd;
			ipEnd = temp;
			$ipBeginning_shelf.val(ipBeginning);
			$ipEnd_shelf.val(ipEnd);
		}
		
		// 当IP起始索引或结束索引发生改变时，检查“忽略的IP索引”下拉列表框中的值，若存在索引范围之外的值则去除
		if (!values) {
			var selections = $ignores_shelf.val();
			values = new Array();
			if (selections) {
				if ($.isArray(selections)) {
					for (var i = 0; i < selections.length; i++) {
						if (selections[i] >= ipBeginning && selections[i] <= ipEnd) {
							values[i] = selections[i];
						}
					}
				} else {
					if (selections >= ipBeginning && selections <= ipEnd) {
						values[0] = selections;
					}
				}
			}
		}
		
		// 重建“忽略的IP索引”下拉框
		$ignores_shelf.empty();
		$ignores_shelf.append($('<option value=""></option>'));
		for (var i = ipBeginning; i <= ipEnd; i++) {
			$ignores_shelf.append($('<option></option>').val(i).html(i));
		}
		$ignores_shelf.val(values);
		$ignores_shelf.chosen('destroy').chosen({width:'100%'});
	}
	
	// 初始化表单验证
	function initValidators() {
		var validIcon = "<i class='fa fa-times-circle'></i> ";
		$byShelfForm.validate({
			rules: {
				ipBeginning: {required: true, digits:true, min: 1, max:254},
				ipEnd: {required: true, digits:true, min: 1, max:254}
			},
			messages: {
				ipBeginning: 'IP起始索引必须为1-254的整数。',
				ipEnd: 'IP结束索引必须为1-254的整数。'
			}
		});
	}
	
	function initLinstener() {
		// 打开固件选择窗口
		$btnSelectFirmware_shelf.on('click', function() {
			initChosenTypeData($chosenFirmwareTypeList.val());
			$firmwareTable.bootstrapTable('refresh');
			$firmwareDialog.modal('show');
			by = 1;
		});
		
		// 打开机架选择窗口
		$btnSelectShelf_shelf.on('click', function() {
			initChosenTypeData($chosenShelfTypeList.val());
			$shelfTable.bootstrapTable('refresh');
			$shelfDialog.modal('show');
		});
		
		// 确认选择固件
		$btnFirmwareSelect.on('click', function() {
			var selections = $firmwareTable.bootstrapTable('getSelections');
			if (selections.length == 0) {
				toastr.warning('请选择一个固件。', '提示');
				return;
			}
			firmwareSelected(selections[0]);
			$firmwareDialog.modal('hide');
		});
		
		// 确认选择机架
		$btnShelfSelect.on('click', function() {
			var selections = $shelfTable.bootstrapTable('getSelections');
			if (selections.length == 0) {
				toastr.warning('请选择一个机架。', '提示');
				return;
			}
			shelfSelected(selections[0]);
			$shelfDialog.modal('hide');
		});
		
		// 按机架：清空按钮
		$btnReset_shelf.on('click', function() {
			$firmwareTypeId_shelf.val('');
			$firmwareNameDisplay_shelf.text('');
			$firmwareVersionDisplay_shelf.text('');
			$firmwareTypeDisplay_shelf.text('');
			$firmwareId_shelf.val('');
			$shelfTypeId_shelf.val('');
			$shelfNameDisplay_shelf.text('');
			$ipBlockDisplay_shelf.text('');
			$shelfTypeDisplay_shelf.text('');
			$minerShelfId_shelf.val('');
			$minerShelfName_shelf.val('');
			$ipBlock_shelf.val('');
			$ipBeginning_shelf.val('');
			$ipEnd_shelf.val('');
			buildIgnoresChosen(null);
			$clean_shelf.attr('checked', false);
			$force_shelf.attr('checked', true);
			compareMinerType();
		});
		
		// 按机架：提交按钮
		$btnSave_shelf.on('click', function() {
			var valid = $byShelfForm.validate().form();
			if (!$firmwareId_shelf.val()) {
				valid &= false;
				$firmwareNameTip_shelf.show();
			}
			if (!$minerShelfId_shelf.val()) {
				valid &= false;
				$shelfNameTip_shelf.show();
			}
			if (!valid) {
				return;
			}
			doSubmitByShelf();
		});
		
		// 起始IP索引-值改变时重建“忽略的IP索引”下拉列表框
		$ipBeginning_shelf.on('change', function() {
			buildIgnoresChosen();
		});
		// 结束IP索引-值改变时重建“忽略的IP索引”下拉列表框
		$ipEnd_shelf.on('change', function() {
			buildIgnoresChosen();
		});
		
		$btnSelectFirmware_ip.on('click', function() {
			initChosenTypeData($chosenFirmwareTypeList.val());
			$firmwareTable.bootstrapTable('refresh');
			$firmwareDialog.modal('show');
			by = 2;
		});
		
		// 按IP：清空按钮
		$btnReset_ip.on('click', function() {
			$firmwareNameDisplay_ip.text('');
			$firmwareVersionDisplay_ip.text('');
			$firmwareTypeDisplay_ip.text('');
			$firmwareId_ip.val('');
			$clean_ip.attr('checked', false);
			$force_ip.attr('checked', true);
			$fileNameDisplay.text('');
			uploader.reset();
		});
		
		// 按IP：提交按钮
		$btnSave_ip.on('click', function() {
			var valid = true;
			if (!$firmwareId_ip.val()) {
				valid &= false;
				$firmwareNameTip_ip.show();
			}
			var files = uploader.getFiles();
			if (!files || files.length <= 0) {
				valid &= false;
				$ipListTip_ip.show();
			}
			if (!valid) {
				return;
			}
			doSubmitByIP();
		});
		
		// 固件查询条件：值改变时刷新数据表格
		$chosenFirmwareTypeList.on('change', function() {
			$firmwareTable.bootstrapTable('refresh');
		});
		$inputFirmwareName.on('change', function() {
			$firmwareTable.bootstrapTable('refresh');
		});
		
		// 机架查询条件：值改变时刷新数据表格
		$inputShelfName.on('change', function() {
			$shelfTable.bootstrapTable('refresh');
		});
		$inputShelfIPBlock.on('change', function() {
			$shelfTable.bootstrapTable('refresh');
		});
		$chosenShelfAreaList.on('change', function() {
			$shelfTable.bootstrapTable('refresh');
		});
		$chosenShelfTypeList.on('change', function() {
			$shelfTable.bootstrapTable('refresh');
		});
		$chosenShelfEnableList.on('change', function() {
			$shelfTable.bootstrapTable('refresh');
		});
	}
	
	// 查询条件中的 “矿机类型”下拉列表框-添加数据
	function initChosenTypeData(selection) {
		$.ajax({
			url: 'types',
			type: 'GET',
			dataType: 'json',
			contentType: "application/json; charset=utf-8",
			success: function(result) {
				if (result.success) {
					$chosenFirmwareTypeList.empty();
					$chosenFirmwareTypeList.append($('<option></option>').val('').html(''));
					$chosenShelfTypeList.empty();
					$chosenShelfTypeList.append($('<option></option>').val('').html(''));
					var allDatas = result.rows;
					for (var i = 0; i < allDatas.length; i++) {
						var data = allDatas[i];
						$chosenFirmwareTypeList.append($('<option></option>').val(data.id).html(data.name));
						$chosenShelfTypeList.append($('<option></option>').val(data.id).html(data.name));
					}
					$chosenFirmwareTypeList.val(selection);
					$chosenFirmwareTypeList.chosen('destroy').chosen({allow_single_deselect: true, width:'100px'});
					$chosenShelfTypeList.val(selection);
					$chosenShelfTypeList.chosen('destroy').chosen({allow_single_deselect: true, width:'100px'});
				} else {
					toastr.error(result.message, '矿机类型下拉列表数据获取失败');
				}
			},
			error: function(request, status, error) {
				toastr.error(error, '矿机类型下拉列表数据获取失败');
			}
		});
	}
	
	// 查询条件中的 “所属矿区”下拉列表-添加数据
	function initChosenAreaData(selection) {
		$.ajax({
			url: 'areas',
			type: 'GET',
			dataType: 'json',
			contentType: "application/json; charset=utf-8",
			success: function(result) {
				if (result.success) {
					$chosenShelfAreaList.empty();
					$chosenShelfAreaList.append($('<option></option>').val('').html(''));
					var allDatas = result.rows;
					for (var i = 0; i < allDatas.length; i++) {
						var data = allDatas[i];
						$chosenShelfAreaList.append($('<option></option>').val(data.id).html(data.name));
					}
					$chosenShelfAreaList.val(selection);
					$chosenShelfAreaList.chosen('destroy').chosen({allow_single_deselect: true, width:'150px'});
				} else {
					toastr.error(result.message, '矿区下拉列表数据获取失败');
				}
			},
			error: function(request, status, error) {
				toastr.error(error, '矿区下拉列表数据获取失败');
			}
		});
	}
	
	// 初始化页面控件
	function initWidgets() {
		$iChecks.iCheck({checkboxClass:'icheckbox_square-green',radioClass:'iradio_square-green'});
		$ignores_shelf.chosen({width:'100%'});
		
		initFirmwareTable();
		initShelfTable();
		
		initChosenTypeData('');
		initChosenAreaData('');
	}
	
	function initShelfTable() {
		// 查询条件：“所属矿区”下拉列表-初始化
		$chosenShelfAreaList.chosen({allow_single_deselect: true, width:'150px'});
		// 查询条件：“矿机类型”下拉列表框-初始化
		$chosenShelfTypeList.chosen({allow_single_deselect: true, width:'100px'});
		// 查询条件：“可用状态”下拉列表框
		$chosenShelfEnableList.val('1');
		$chosenShelfEnableList.chosen({allow_single_deselect: true, width:'80px'});
		
		// 表格
		$shelfTable.bootstrapTable({
			height: 300,
			url: 'shelfs',
			queryParams: function(params) { // 构建表格查询参数
				var areaId = $chosenShelfAreaList.val();
				var minerTypeId = $chosenShelfTypeList.val();
				var name = $inputShelfName.val();
				var ipBlock = $inputShelfIPBlock.val();
				var enable = null;
				var enableVal = $chosenShelfEnableList.val();
				if (enableVal == '1') {
					enable = true;
				} else if (enableVal == '-1') {
					enable = false;
				} else {
					enable = null;
				}
				var params = {
								areaId: areaId,
								minerTypeId: minerTypeId,
								name: name,
								ipBlock: ipBlock,
								enable: enable,
								sort: params.sort,
								order: params.order
							};
				return params;
			},
			idField: 'id',
			//clickToSelect: true,
			pagination: false,
			sidePagination: 'server',
			showRefresh: true,
			showToggle: true,
			showColumns: true,
			singleSelect: true,
			toolbar: '#shelfTableToolbar',
			iconSize: 'outline',
			icons: {
				paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
	            paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
	            refresh: 'glyphicon-repeat',
				toggle: 'glyphicon-list-alt',
				columns: 'glyphicon-list',
	            detailOpen: 'glyphicon-plus icon-plus',
	            detailClose: 'glyphicon-minus icon-minus'
			},
			rowStyle: function(row, index) { // 机架所属矿区不可用时高亮显示整行
				if (row.areaEnable == false) {
					return {classes: 'warning'};
				}
				return {};
			},
			columns: [
				{
					field: 'checkStatus',
					checkbox: true,
					align: 'center'
				},
				{
					field: 'id',
					title: 'ID',
					align: 'center',
					sortable: true,
					visible: false
				},
				{
					field: 'areaName',
					title: '所属矿区',
					halign: 'center',
					align: 'left',
				},
				{
					field: 'name',
					title: '机架名称',
					halign: 'center',
					align: 'left',
					sortable: true
				},
				{
					field: 'minerTypeName',
					title: '矿机类型',
					halign: 'center',
					align: 'left',
				},
				{
					field: 'ipBlock',
					title: 'IP段',
					halign: 'center',
					align: 'left',
					sortable: true,
				},
				{
					field: 'ipBeginning',
					title: 'IP起始索引',
					halign: 'center',
					align: 'right',
				},
				{
					field: 'ipEnd',
					title: 'IP结束索引',
					halign: 'center',
					align: 'right',
				},
				{
					field: 'ignores',
					title: '忽略的IP索引',
					halign: 'center',
					align: 'right',
					formatter: function(value, row, index) {
						if (!value) {
							return value;
						}
						return '<button class="btn btn-default" data-toggle="tooltip" data-placement="bottom" title="' + value + '">' + value.length + '个</button>'
					}
				},
				{
					field: 'minerCount',
					title: '矿机数量',
					align: 'center',
					align: 'right',
					formatter: function(value, row, index) {
						var end = row.ipEnd ? row.ipEnd : 0;
						var beginng = row.ipBeginning ? row.ipBeginning : 0;
						var ignore = row.ignores ? row.ignores.length : 0;
						return end - beginng + 1 - ignore;
					}
				},
				{
					field: 'enable',
					title: '可用状态',
					align: 'center',
					sortable: true,
					sortName: 'enable',
					formatter: function(value, row, index) {
						return value == true ? '<img src="../img/icon/icon-enabled.png" width="20" height="20" />' : '<img src="../img/icon/icon-disabled.png" width="20" height="20" />';
					}
				},
				{
					field: 'remark',
					title: '备注',
					halign: 'center',
					align: 'left'
				}
			]
		});
	}
	
	function initFirmwareTable() {
		// 查询条件：矿机类型下拉列表框
		$chosenFirmwareTypeList.chosen({allow_single_deselect: true, width:'100px'});
		
		// 固件选择表格
		$firmwareTable.bootstrapTable({
			height: 300,
			url: 'firmwares',
			queryParams: function(params) { // 构建表格查询参数
				var minerTypeId = $chosenFirmwareTypeList.val();
				var name = $inputFirmwareName.val();
				var params = {
								minerTypeId: minerTypeId,
								name: name,
								sort: params.sort,
								order: params.order
							};
				return params;
			},
			idField: 'id',
			//clickToSelect: true,
			pagination: false,
			sidePagination: 'server',
			showRefresh: true,
			showToggle: true,
			showColumns: true,
			singleSelect: true,
			toolbar: '#firmwareTableToolbar',
			iconSize: 'outline',
			icons: {
				paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
	            paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
	            refresh: 'glyphicon-repeat',
				toggle: 'glyphicon-list-alt',
				columns: 'glyphicon-list',
	            detailOpen: 'glyphicon-plus icon-plus',
	            detailClose: 'glyphicon-minus icon-minus'
			},
			columns: [
				{
					field: 'checkStatus',
					checkbox: true,
					align: 'center'
				},
				{
					field: 'id',
					title: 'ID',
					align: 'center',
					sortable: true,
					visible: false
				},
				{
					field: 'name',
					title: '固件名称',
					halign: 'center',
					align: 'left',
					sortable: true
				},
				{
					field: 'version',
					title: '固件版本',
					align: 'center',
					sortable: true,
				},
				{
					field: 'minerTypeName',
					title: '矿机类型',
					halign: 'center',
					align: 'left',
				},
				{
					field: 'remark',
					title: '备注',
					halign: 'center',
					align: 'left',
					editable: {
						type: 'text',
						validate: function(value) {
							value = $.trim(value);
							if (!value && value.length > 300) {
								return '备注不能超过300个字符。';
							}
							return '';
						}
					}
				},
				{
					field: 'insertTime',
					title: '上传时间',
					align: 'center',
					sortable: true,
					formatter: function(value, row, index) {
						return new Date(value).format('yyyy-MM-dd hh:mm:ss');
					}
				}
			]
		});
	}
	
	// 设置文件上传控件按钮尺寸
	function initUploaderSize() {
		var children = $('#btnFilePicker').children();
		for (var i = 0; i < children.length; i++) {
			$(children[i]).css({width:300, height:38});
		}
	}
	
	// 初始化文件上传
	function initUploader() {
		var fileLimit = 10 * 1024 * 1024; // 10 M
	    uploader = WebUploader.create({
	    	swf: '../js/plugins/webuploader/Uploader.swf',
	    	server: 'updates/ip',
	    	method: 'POST',
	    	pick: {
	    		id: '#btnFilePicker',
	    		multiple: false
	    	},
	        disableGlobalDnd: true,
	        fileNumLimit: 1,
	        fileSizeLimit: fileLimit,
	        fileSingleSizeLimit: fileLimit,
	        accept: {
	        	title: 'Text',
	        	extensions: 'txt',
	        	mimeTypes: 'text/plain'
	        }
	    });
	    
	    initUploaderSize();
	    
	    uploader.on('fileQueued', function(file) {
	    	$fileNameDisplay.text(file.name);
	    	$ipListTip_ip.hide();
	    });
	    
	    uploader.on('beforeFileQueued', function(file) {
	    	uploader.reset();
	    	return true;
	    });
	    
	    uploader.on('error', function(error) {
	    	if ('Q_EXCEED_SIZE_LIMIT' == error) {
	    		toastr.error('所选文件不能大于10M', '文件错误');
	    	}
	    	if ('Q_TYPE_DENIED' == error) {
	    		toastr.error('只能选择txt格式的文本文件', '文件错误');
	    	}
	    });
	    
	    uploader.on('uploadProgress', function(file, percentage) {
	    	loadmask.updateProgress(percentage);
	    });
	    
	    uploader.on('uploadSuccess', function(file, result) {
	    	if (result.success) {
	    		toastr.success(result.message, '提交成功');
	    	} else {
	    		toastr.error(result.message, '提交失败');
	    	}
	    });
	    
	    uploader.on('uploadError', function(file, reason) {
	    	toastr.error(reason, '提交失败');
	    });
	    
	    uploader.on('uploadComplete', function(file) {
	    	uploader.reset();
	    	$fileNameDisplay.text('');
	    	loadmask.hide();
	    });
	}
});